import school
import grade
import classroom
import teacher 
import student
import csv


"""
parse the csv file, and load all objects into the place holder 
schools 
"""
def load_csv(schools, infile):
    reader = csv.reader( open(infile, newline=''),
                         delimiter = ',' )
    
    wgen = school.school("100", "WGen School")
    schools[wgen.id] = wgen
    
    #walk through each row, load classroom, teacher1, teacher2, and student 
    #into objects                     
    for row in reader:
        if(len(row)):
            grade_id = row[0].strip()[0]
            
            if( not wgen.grades.get(grade_id) ):
                curr_grade = grade.grade(grade_id) 
                wgen.add_grade(curr_grade)
            else:
                curr_grade = wgen.grades[grade_id]
            
            classroom_id = row[0].strip()
            if(not curr_grade.classrooms.get(classroom_id)):
                curr_classroom = classroom.classroom(classroom_id, row[1].strip())
                curr_grade.add_classroom(curr_classroom)
            else:
                curr_classroom = curr_grade.classrooms.get(classroom_id)
                
            if(row[2].strip()):
                curr_classroom.add_teacher(teacher.teacher(row[2].strip(), row[3].strip(), row[4].strip()))
            if(row[5].strip()):
                curr_classroom.add_teacher(teacher.teacher(row[5].strip(), row[6].strip(), row[7].strip()))
            if(row[8].strip()):
                curr_classroom.add_student(student.student(row[8].strip(), row[9].strip(), row[10].strip(), row[11].strip()))
                
            
"""
output row representations of object to csv file
"""
def writecsv(schools, outfile):
    writer = csv.writer(open(outfile, 'w'), delimiter=',' )
    
    for school_id in sorted (schools.keys()):
        grades = schools[school_id].grades
        for grade_id in sorted (grades.keys()):
            row = []    
            grade = grades[grade_id]
            for classroom_id in sorted (grade.classrooms.keys()):
                classroom = grade.classrooms[classroom_id]
                
                row.append(classroom.id)
                row.append(classroom.name)
                
                teachers = classroom.teachers
                t_ids = sorted(classroom.teachers.keys())
                
                i = 0
                while(i<2):
                    if(len(t_ids)>i):
                        row.append(t_ids[i])
                        row.append(teachers[t_ids[i]].last_name)
                        row.append(teachers[t_ids[i]].first_name)
                    else:
                        row.append("")
                        row.append("")
                        row.append("")
                    i = i+1    
                
                
                for s_id in sorted (classroom.students.keys()):
                    row.append(s_id)
                    row.append(classroom.students[s_id].last_name)
                    row.append(classroom.students[s_id].first_name)
                    row.append(classroom.students[s_id].grade)
                    #print(row)
                    writer.writerow(row)
                    row = row[:-4]
                    
                if(len(classroom.students)==0):
                    row = row + ["", "", "",  ""]
                    writer.writerow(row)
                    row = row[:-4]
                #empty the list    
                del row[:] 

